<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>编辑管理员</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body" style="margin-left: 50px">
    <form class="layui-form">
        <input type="hidden" id="userId" name="userId" value="${editUser.userId}">
        <div class="layui-form-item">
            <label for="userName" class="layui-form-label">
                会员名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="userName" name="userName" value="${editUser.userName}" required=""
                       lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="userPhone" class="layui-form-label">
                手机号码
            </label>
            <div class="layui-input-inline">
                <input type="number" id="userPhone" name="userPhone" value="${editUser.userPhone}" required=""
                       lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="userPhone" class="layui-form-label">
                管理员角色
            </label>
            <div id="roles">
                        <%--<input type="checkbox" name="like[write]" title="写作">
                        <input type="checkbox" name="like[read]" title="阅读" checked>
                        <input type="checkbox" name="like[dai]" title="发呆">--%>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <a class="layui-btn layui-btn-normal" id="showBtn" onclick="showPasswordPart()">修改密码</a>
        </div>
        <div id="passwordPart" style="display: none">

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">
                确认编辑
            </button>
        </div>
    </form>
</div>
<script>
    var isShow = false;

    function showPasswordPart() {
        if (!isShow) {
            $("#passwordPart").append('<div class="layui-form-item">\n' +
                '                <label for="oldPassword" class="layui-form-label">\n' +
                '                    原密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="oldPassword" name="oldPassword" value="" lay-verify="password"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>' +
                '<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="userPassword" class="layui-form-label">\n' +
                '                    新密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="userPassword" name="userPassword" value="" lay-verify="password"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="layui-form-item">\n' +
                '                <label for="userPassword2" class="layui-form-label">\n' +
                '                    确认密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="userPassword2" name="userPassword2" value="" lay-verify="password2"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>')
            $("#showBtn").text("隐藏修改密码");
            $("#passwordPart").show();
            isShow = true;
        } else {
            $("#passwordPart").empty();
            $("#showBtn").text("修改密码");
            isShow = false;
        }
    };

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        if ($("#oldPassword").val() != null || $("#oldPassword").val() != "") {
            if ($('#userPassword').val() != null || $('#userPassword').val() != "") {
                //自定义验证规则
                form.verify({
                    password: [/(.+){6,12}$/, '密码必须6到12位']
                    , password2: function () {
                        if ($('#userPassword').val() != $('#userPassword2').val()) {
                            return '两次密码不一致';
                        }
                    }
                });
            }
        }

        $.ajax({
            type: "post",
            url: "${pageContext.request.contextPath}/role/findRole",
            dataType: "json",
            success: function (data) {
                if (data.code === 200){
                    $('#roles').empty();
                    $(data.data).each(function () {
                        $('#roles').append('<input type="checkbox" id="role'+this.roleId+'" name="role'+this.roleId+'" value="'+this.roleId+'" title="'+this.roleName+'">')
                    });
                    $.ajax({
                        type: "post",
                        url: "${pageContext.request.contextPath}/role/findRoleById",
                        dataType: "json",
                        data:{userId:$("#userId").val()},
                        success: function (data) {
                            $(":checkbox").attr("checked", false);
                            if (data.code === 200){
                                $(data.data).each(function () {
                                    $("#role"+this.roleId).attr("checked", true);
                                });
                                // 重新渲染
                                form.render('checkbox');
                            }
                        }
                    });

                }
            }
        });


        //监听提交
        form.on('submit(edit)', function (data) {
            console.log(data);
            //发异步，把数据提交给java
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/admin/edit/do",
                data: data.field,
                dataType: "json",
                success: function (data) {
                    layer.msg(data.msg, function () {
                        if (data.code === 200) {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                    });
                }
            });
            return false;
        });

    });


</script>

</body>

</html>